package com.app.newcsc.fragment.my.task;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;

import com.app.newcsc.app.AppApplication;
import com.app.newcsc.data.DemoRepository;
import com.app.newcsc.entity.TaskInfoEntity;
import com.app.newcsc.entity.WalletEntity;

import java.util.HashMap;
import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.http.BaseResponse;
import me.goldze.mvvmhabit.http.ResponseThrowable;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * @author: woking
 * @date: 2021/6/15
 * @description：
 */
public class TaskViewModel extends BaseViewModel<DemoRepository> {

    public SingleLiveEvent<Integer> type = new SingleLiveEvent<>();
    public SingleLiveEvent<Boolean> isOrder = new SingleLiveEvent<>();

    public SingleLiveEvent<TaskInfoEntity> mTaskInfoEntity = new SingleLiveEvent<>();//监听买卖按钮

    public TaskViewModel(@NonNull Application application, DemoRepository repository) {
        super(application, repository);
    }

    /**
     * 任务包 产品列表
     */
//    public void financeProduct()
//    {
//        model.financeProduct()
//                .compose(RxUtils.schedulersTransformer()) //线程调度
//                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
//                .doOnSubscribe(this)//请求与ViewModel周期同步
//                .doOnSubscribe(new Consumer<Disposable>() {
//                    @Override
//                    public void accept(Disposable disposable) throws Exception {
////                        showDialog("正在请求...");
//                    }
//                })
//                .subscribe(new DisposableObserver<BaseResponse<TaskInfoEntity>>() {
//                    @Override
//                    public void onNext(BaseResponse<TaskInfoEntity> response) {
//                        Log.e("TAG","============financeProduct=====onNext========");
//                        //请求成功
//                        if (response.getCode() == 200) {
////                            mFundsBalanceEntity.postValue(response.getData());
//
////                            for (DemoEntity.ItemsEntity entity : response.getResult().getItems()) {
////                                OrderRecordTabItemViewModel itemViewModel = new OrderRecordTabItemViewModel(OrderRecordTabViewModel.this, entity);
////                                //双向绑定动态添加Item
////                                observableList.add(itemViewModel);
////                            }
//                        } else {
//                            //code错误时也可以定义Observable回调到View层去处理
//                            ToastUtils.showShort("数据错误");
//                        }
//                    }
//                    @Override
//                    public void onError(Throwable throwable) {
//                        Log.e("TAG","============financeProduct=====onError========");
//                        if (throwable instanceof ResponseThrowable) {
//                            ToastUtils.showShort(((ResponseThrowable) throwable).message);
//                        }
//                    }
//                    @Override
//                    public void onComplete() {
//                        Log.e("TAG","============financeProduct=====onComplete========");
//                    }
//                });
//    }
    /**
     * 任务包 产品详情
     */
    public void financeProductDetil(String id)
    {
        model.financeProductDetail(id)
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .doOnSubscribe(this)//请求与ViewModel周期同步
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
//                        showDialog("正在请求...");
                    }
                })
                .subscribe(new DisposableObserver<BaseResponse<TaskInfoEntity>>() {
                    @Override
                    public void onNext(BaseResponse<TaskInfoEntity> response) {
                        Log.e("TAG","============financeProductDetil=====onNext========");
                        //请求成功
                        if (response.getCode() == 200) {
//                            mFundsBalanceEntity.postValue(response.getData());

//                            for (DemoEntity.ItemsEntity entity : response.getResult().getItems()) {
//                                OrderRecordTabItemViewModel itemViewModel = new OrderRecordTabItemViewModel(OrderRecordTabViewModel.this, entity);
//                                //双向绑定动态添加Item
//                                observableList.add(itemViewModel);
//                            }
                        } else {
                            //code错误时也可以定义Observable回调到View层去处理
                            ToastUtils.showShort("数据错误");
                        }
                    }
                    @Override
                    public void onError(Throwable throwable) {
                        Log.e("TAG","============financeProductDetil=====onError========");
                        if (throwable instanceof ResponseThrowable) {
                            ToastUtils.showShort(((ResponseThrowable) throwable).message);
                        }
                    }
                    @Override
                    public void onComplete() {
                        Log.e("TAG","============financeProductDetil=====onComplete========");
                    }
                });
    }
    /**
     * 任务包 订单列表
     */
//    public void financeProductOrders()
//    {
//        model.financeProductOrders("1","product")//status 状态:1运行中,2到期
//                .compose(RxUtils.schedulersTransformer()) //线程调度
//                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
//                .doOnSubscribe(this)//请求与ViewModel周期同步
//                .doOnSubscribe(new Consumer<Disposable>() {
//                    @Override
//                    public void accept(Disposable disposable) throws Exception {
////                        showDialog("正在请求...");
//                    }
//                })
//                .subscribe(new DisposableObserver<BaseResponse<TaskInfoEntity>>() {
//                    @Override
//                    public void onNext(BaseResponse<TaskInfoEntity> response) {
//                        Log.e("TAG","============financeProductOrders=====onNext========");
//                        //请求成功
//                        if (response.getCode() == 200) {
////                            mFundsBalanceEntity.postValue(response.getData());
//
////                            for (DemoEntity.ItemsEntity entity : response.getResult().getItems()) {
////                                OrderRecordTabItemViewModel itemViewModel = new OrderRecordTabItemViewModel(OrderRecordTabViewModel.this, entity);
////                                //双向绑定动态添加Item
////                                observableList.add(itemViewModel);
////                            }
//                        } else {
//                            //code错误时也可以定义Observable回调到View层去处理
//                            ToastUtils.showShort("数据错误");
//                        }
//                    }
//                    @Override
//                    public void onError(Throwable throwable) {
//                        Log.e("TAG","============financeProductOrders=====onError========");
//                        if (throwable instanceof ResponseThrowable) {
//                            ToastUtils.showShort(((ResponseThrowable) throwable).message);
//                        }
//                    }
//                    @Override
//                    public void onComplete() {
//                        Log.e("TAG","============financeProductOrders=====onComplete========");
//                    }
//                });
//    }


    /**
     * 任务包 订单详情
     */
    public void financeProductOrderDetails()
    {
        model.financeProductOrderDetails("9","")
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .doOnSubscribe(this)//请求与ViewModel周期同步
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
//                        showDialog("正在请求...");
                    }
                })
                .subscribe(new DisposableObserver<BaseResponse<TaskInfoEntity>>() {
                    @Override
                    public void onNext(BaseResponse<TaskInfoEntity> response) {
                        Log.e("TAG","============financeProductOrderDetails=====onNext========");
                        //请求成功
                        if (response.getCode() == 200) {
//                            mFundsBalanceEntity.postValue(response.getData());

//                            for (DemoEntity.ItemsEntity entity : response.getResult().getItems()) {
//                                OrderRecordTabItemViewModel itemViewModel = new OrderRecordTabItemViewModel(OrderRecordTabViewModel.this, entity);
//                                //双向绑定动态添加Item
//                                observableList.add(itemViewModel);
//                            }
                        }
                    }
                    @Override
                    public void onError(Throwable throwable) {
                        Log.e("TAG","============financeProductOrderDetails=====onError========");
//                        if (throwable instanceof ResponseThrowable) {
//                            ToastUtils.showShort(((ResponseThrowable) throwable).message);
//                        }
                    }
                    @Override
                    public void onComplete() {
                        Log.e("TAG","============financeProductOrderDetails=====onComplete========");
                    }
                });
    }

    public void otherAssets()
    {
        HashMap<String, Object> map = new HashMap<>();
        map.put("lang","zh-cn");
        model.otherAssets(map)
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .doOnSubscribe(this)//请求与ViewModel周期同步
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
//                        showDialog("正在请求...");
                    }
                })
                .subscribe(new DisposableObserver<BaseResponse<TaskInfoEntity>>() {
                    @Override
                    public void onNext(BaseResponse<TaskInfoEntity> response) {
                        Log.e("TAG","============fundsBalance=====onNext========"+response.getData().todayRecharge);
                        //请求成功
                        if (response.getCode() == 200) {
//                            mFundsBalanceEntity.postValue(response.getData());

//                            for (DemoEntity.ItemsEntity entity : response.getResult().getItems()) {
//                                OrderRecordTabItemViewModel itemViewModel = new OrderRecordTabItemViewModel(OrderRecordTabViewModel.this, entity);
//                                //双向绑定动态添加Item
//                                observableList.add(itemViewModel);
//                            }
                        } else {
                            //code错误时也可以定义Observable回调到View层去处理
                            ToastUtils.showShort("数据错误");
                        }
                    }
                    @Override
                    public void onError(Throwable throwable) {
                        Log.e("TAG","============fundsBalance=====onError========");
                        if (throwable instanceof ResponseThrowable) {
                            ToastUtils.showShort(((ResponseThrowable) throwable).message);
                        }
                    }
                    @Override
                    public void onComplete() {
                        Log.e("TAG","============fundsBalance=====onComplete========");
                    }
                });
    }

    public SingleLiveEvent<List<WalletEntity>> mWalletEntitys = new SingleLiveEvent<>();
    /**
     * 钱包余额
     */
    public void otcWallet(String include)
    {
        model.otcWallet("coin")
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .doOnSubscribe(this)//请求与ViewModel周期同步
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
//                        showDialog("正在请求...");
                    }
                })
                .subscribe(new DisposableObserver<BaseResponse<List<WalletEntity>>>() {
                    @Override
                    public void onNext(BaseResponse<List<WalletEntity>> response) {
                        if(response != null && response.getData() != null && response.getData().size() > 0)
                        {
                            //http://newcsc.chaishows.com/api/finance/product_order?filter%5Bstatus%5D=1&include=product&page=1
                            Log.e("TAG","============otcWallet=====onNext========"+response.getData().size());

                            ((AppApplication)AppApplication.getInstance()).setOtcWalletAll(response.getData());

                            mWalletEntitys.postValue(response.getData());
                        }
                    }
                    @Override
                    public void onError(Throwable throwable) {
                        Log.e("TAG","============otcWallet=====onError========");
//                        if (throwable instanceof ResponseThrowable) {
//                            ToastUtils.showShortSafe(((ResponseThrowable) throwable).message);
//                        }
//                        dismissDialog();
//                        //请求刷新完成收回
//                        uc.finishRefreshing.call();
                    }
                    @Override
                    public void onComplete() {
                        Log.e("TAG","============otcWallet=====onComplete========");
//                        dismissDialog();
//                        //请求刷新完成收回
//                        uc.finishRefreshing.call();
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}